package com.util.gzp.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**
 * ClassName: CORSConfiguration
 * Package: com.xuexi0311.config
 *
 * @author: guanzepeng
 * @Date: 2021/3/25 11:32 星期四
 * @Description: 配置跨域处理请求
 */
@SuppressWarnings("AlibabaClassNamingShouldBeCamel")
@Configuration
public class CORSConfiguration extends WebMvcConfigurationSupport {

	@Override
	protected void addCorsMappings(CorsRegistry registry) {

		registry.addMapping("/**")
						.allowedOrigins("*")
						.allowedMethods("GET","HEAD","POST","PUT","DELETE","OPTIONS")
						.allowedHeaders("*")
						.exposedHeaders("access-control-allow-headers",
														"access-control-allow-methods",
														"access-control-allow-origin",
														"access-control-max-age",
														"X-Frame-Options")
						.allowCredentials(true).maxAge(3600);
		super.addCorsMappings(registry);
	}

	/**
	 * 发现如果继承了WebMvcConfigurationSupport，则在yml中配置的相关内容会失效。 需要重新
	 指定静态资源
	 *
	 * @param registry
	 *
	 * */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/webjars/**").addResourceLocations(
				"classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("/**").addResourceLocations(
				"classpath:/static/");
		registry.addResourceHandler("index.html").addResourceLocations(
				"classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("/swagger-ui/index.html").addResourceLocations(
				"classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("/swagger-ui/swagger-ui-bundle.js").addResourceLocations(
				"classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("/swagger-ui/swagger-ui-standalone-preset.js").addResourceLocations(
				"classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("/swagger-ui/springfox.js").addResourceLocations(
				"classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("/swagger-resources/configuration/ui").addResourceLocations(
				"classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("/swagger-resources/configuration/security").addResourceLocations(
				"classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("/swagger-resources").addResourceLocations(
				"classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("/sockjs-node/info").addResourceLocations(
				"classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("/favicon.ico").addResourceLocations(
				"classpath:/META-INF/resources/webjars/");
		registry.addResourceHandler("swagger-ui.html").addResourceLocations(
				"classpath:/META-INF/resources/");
		registry.addResourceHandler("/swagger-resources/configuration/ui").addResourceLocations(
				"classpath:/META-INF/resources/");
		registry.addResourceHandler("/swagger-resources/configuration/security").addResourceLocations(
				"classpath:/META-INF/resources/");
		registry.addResourceHandler("/v3/api-docs").addResourceLocations(
				"classpath:/META-INF/resources/");
		registry.addResourceHandler("/swagger-resources").addResourceLocations(
				"classpath:/META-INF/resources/");
		registry.addResourceHandler("/swagger-ui/**").addResourceLocations(
				"classpath:/META-INF/resources/");
		super.addResourceHandlers(registry);
	}

}
